package com.sam4cloud.common.core.model;

import com.sam4cloud.common.core.cons.HttpCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author: Sam
 * @Date: 2022/11/3 14:28
 * @Description: 响应信息主体
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> ok() {
        return buildResult(HttpCode.SC_OK, null, null);
    }

    public static <T> R<T> ok(T data) {
        return buildResult(HttpCode.SC_OK, null, data);
    }

    public static <T> R<T> ok(T data, String msg) {
        return buildResult(HttpCode.SC_OK, msg, data);
    }

    public static <T> R<T> failed() {
        return buildResult(HttpCode.SC_INTERNAL_SERVER_ERROR, null, null);
    }

    public static <T> R<T> failed(String msg) {
        return buildResult(HttpCode.SC_INTERNAL_SERVER_ERROR, msg, null);
    }

    public static <T> R<T> failed(T data) {
        return buildResult(HttpCode.SC_INTERNAL_SERVER_ERROR, null, data);
    }

    public static <T> R<T> failed(T data, String msg) {
        return buildResult(HttpCode.SC_INTERNAL_SERVER_ERROR, msg, data);
    }

    public static <T> R<T> buildResult(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }

}
